﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Salon
{
    class TransferService
    {
        public TransferService()
        { }

        public Transfer saveTransfer(Transfer trans)
        {
            String insertQuery = "INSERT INTO transfer VALUES(@car_id,@salon_rec,@salon_sen,@order_date," +
            "@ship_date,@receive_date,@state,1)";

            String updateQuery = "UPDATE transfer SET car_id=@car_id,salon_rec=@salon_rec,salon_sen=@salon_sen,order_date=@order_date," +
            "ship_date=@ship_date,receive_date=@receive_date,state=@state,active=1 " +
            " WHERE id=@id";

            SqlCommand cmd = SqlService.getInstance().getCommand(( trans.Id < 1) ? insertQuery : updateQuery);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@car_id", trans.Car.Id);
            cmd.Parameters.AddWithValue("@salon_rec", trans.SalonRec.Id);
            cmd.Parameters.AddWithValue("@salon_sen", trans.SalonSen.Id);
            cmd.Parameters.AddWithValue("@order_date", trans.OrderDate.Date);
            cmd.Parameters.AddWithValue("@ship_date", trans.ShipCancelDate.Date);
            cmd.Parameters.AddWithValue("@receive_date", trans.ReceiveDate.Date);
            cmd.Parameters.AddWithValue("@state", trans.Status);
            if (( trans.Id > 0))
                cmd.Parameters.AddWithValue("@id", trans.Id);
            cmd.ExecuteNonQuery();

            return trans;
        }

        public bool checkIfCarIsTRansfered(Car car)
        {
            String query = "SELECT id from transfer WHERE car_id=@car_id AND state != @status1 AND state !=@status2";

            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@car_id", car.Id);
            cmd.Parameters.AddWithValue("@status1", TransferState.Received);
            cmd.Parameters.AddWithValue("@status2", TransferState.Cancel);
            Object id = cmd.ExecuteScalar();

            if (id != null)
                return true;
            return false;
        }

        public IList<Transfer> getTransfers(IList<IFilter> filterList)
        {
            IList<Transfer> transfers = new List<Transfer>();
            String query = "SELECT * FROM transfer WHERE 1=1 ";
            query += Global.getConditionString(filterList);

            SqlCommand command = SqlService.getInstance().getCommand(query);
            SqlDataReader reader = command.ExecuteReader();
            CarService cs = ServiceFactory.getCarService();
            AutoSalonService aus = ServiceFactory.getAutoSalonService();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Transfer tr = new Transfer();
                    tr.Id = reader.GetInt32(reader.GetOrdinal("id"));
                    tr.Car = cs.getCarById(reader.GetInt32(reader.GetOrdinal("car_id")));
                    tr.OrderDate = reader.GetDateTime(reader.GetOrdinal("order_date"));
                    tr.ReceiveDate = reader.GetDateTime(reader.GetOrdinal("receive_date"));
                    tr.ShipCancelDate = reader.GetDateTime(reader.GetOrdinal("ship_date"));
                    tr.SalonRec = aus.getAutoSalonById(reader.GetInt32(reader.GetOrdinal("salon_rec")));
                    tr.SalonSen = aus.getAutoSalonById(reader.GetInt32(reader.GetOrdinal("salon_sen")));
                    tr.Status = (TransferState)reader.GetInt32(reader.GetOrdinal("state"));

                    transfers.Add(tr);
                }
            }

            reader.Close();

            return transfers;
        }

        public void deleteTransfer(Transfer tr)
        {
            String query = "DELETE from transfer WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", tr.Id);
            cmd.ExecuteNonQuery();

        }
    }
}
